
package com.hyst.seviceImpl;

import com.hyst.common.Contants;
import com.hyst.common.ResultBean;
import com.hyst.domain.*;
import com.hyst.repository.*;
import com.hyst.service.ProgressReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyst.service.ReportViewService;
import com.hyst.utils.UuidTool;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version ：1.0 1.[2018年09月08日]
 */
@Service
public class ReportViewServiceImpl implements ReportViewService {

    @Autowired
    private ReportViewMapper mapper;

    @Autowired
    private ProgressReportService progressReportService;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private PurchaseCourseViewMapper purchaseCourseViewMapper;

    @Autowired
    private LessonMapper lessonMapper;

    @Autowired
    private LessonLevelMapper lessonLevelMapper;

    @Override
    public Page<ReportView> list(final ReportView reportView) {
        final Page<ReportView> page = PageHelper.startPage(reportView.getPageNum(), reportView.getPageSize());
        if (StringUtils.isNotBlank(reportView.getType()) && "2".equals(reportView.getType())) {
            reportView.setType("Progress Report");
        } else if (StringUtils.isNotBlank(reportView.getType()) && "1".equals(reportView.getType())) {
            reportView.setType("Test Report");
        }
        mapper.select(reportView);
        return page;
    }

    @Override
    public ReportView insert(final ReportView reportView) {
        mapper.insertSelective(reportView);
        return reportView;
    }

    @Override
    public ReportView getByPriKey(final Integer id) {
        final ReportView reportView = mapper.selectByPrimaryKey(id);
        return reportView;
    }


    @Override
    public ReportView getByTypePriKey(final Integer id,Integer type) {
        Map<String,Object> sqlMap = new HashMap<>();
        sqlMap.put("id",id);
        if (type == 1) {
            sqlMap.put("type", "Test Report");
        } else {
            sqlMap.put("type", "Progress Report");
        }

//        final ReportView reportView = mapper.getByType(sqlMap);
        return null;
    }


    @Override
    public ReportView updateSelective(final ReportView reportView) {
        mapper.updateByPrimaryKeySelective(reportView);
        return reportView;
    }

    @Override
    public ReportView update(final ReportView reportView) {
        mapper.updateByPrimaryKey(reportView);
        return reportView;
    }

    @Override
    public ReportView delete(final Integer id) {

        return null;
    }

    @Override
    public ResultBean getByTypePriKey(final Integer id, String type, HttpServletRequest request) {
        try {
            Map<String, Object> sqlMap = new HashMap<>();
            sqlMap.put("id", id);

            ProgressReport result = progressReportService.getByPriKey(id);
            Map studentInfo = studentMapper.getStudentInfo(result.getStudentId());
            result.setStudentName(studentInfo.get("en_name").toString());
//
//            Teacher teacher = (Teacher) request.getSession().getAttribute(Contants.SESSION_USER_INFO);
//            result.setTeacherName(teacher.getName());

            String level = purchaseCourseViewMapper.getLevel(result.getPurchaseCourseId());
            String lessonname = lessonMapper.getMapById(result.getMaterialId()).get("name_en").trim();

            Map<String, String> sqlMapLevel = new HashMap();
            sqlMapLevel.put("lessonId", result.getMaterialId());
            sqlMapLevel.put("levelId", level);
            String select2Data = lessonLevelMapper.getSelect(sqlMapLevel);

            result.setMaterialName(lessonname + " " + select2Data);
            return ResultBean.success(result);
//                sqlMap.put("type", "Progress Report");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultBean.fail("失败");
    }
}
